#ifndef _SIMPLEAVPACKETSERIALIZER_HPP
#define _SIMPLEAVPACKETSERIALIZER_HPP
#include <vector>
#include <cstdint>
#include <cstring>
#include <libavcodec/avcodec.h>
#include <iostream>
class SimpleAVPacketSerializer {
public:
    // 序列化 AVPacket 到连续内存块
    static std::vector<uint8_t> serialize(const AVPacket& pkt) {
        // 计算总大小
        size_t total_size = sizeof(FixedHeader) + pkt.size;
        for (int i = 0; i < pkt.side_data_elems; i++) {
            total_size += sizeof(SideDataHeader) + pkt.side_data[i].size;
        }
        
        std::vector<uint8_t> buffer(total_size);
        uint8_t* ptr = buffer.data();
        
        // 序列化固定头部
        FixedHeader* header = reinterpret_cast<FixedHeader*>(ptr);
        header->pts = pkt.pts;
        header->dts = pkt.dts;
        header->duration = pkt.duration;
        header->pos = pkt.pos;
        header->stream_index = pkt.stream_index;
        header->flags = pkt.flags;
        header->size = pkt.size;
        header->side_data_elems = pkt.side_data_elems;
        
        ptr += sizeof(FixedHeader);
        
        // 序列化主数据
        if (pkt.size > 0) {
            memcpy(ptr, pkt.data, pkt.size);
            ptr += pkt.size;
        }
        
        // 序列化 side data
        for (int i = 0; i < pkt.side_data_elems; i++) {
            SideDataHeader* sd_header = reinterpret_cast<SideDataHeader*>(ptr);
            sd_header->type = static_cast<uint32_t>(pkt.side_data[i].type);
            sd_header->size = pkt.side_data[i].size;
            ptr += sizeof(SideDataHeader);
            
            if (pkt.side_data[i].size > 0) {
                memcpy(ptr, pkt.side_data[i].data, pkt.side_data[i].size);
                ptr += pkt.side_data[i].size;
            }
        }
        
        return buffer;
    }
    
    // 从连续内存块反序列化为 AVPacket
    static AVPacket deserialize(const uint8_t* data, size_t size) {
        if (size < sizeof(FixedHeader)) {
            throw std::runtime_error("Invalid serialized data size");
        }
        
        AVPacket pkt;
        av_init_packet(&pkt);
        
        // 解析固定头部
        const FixedHeader* header = reinterpret_cast<const FixedHeader*>(data);
        pkt.pts = header->pts;
        pkt.dts = header->dts;
        pkt.duration = header->duration;
        pkt.pos = header->pos;
        pkt.stream_index = header->stream_index;
        pkt.flags = header->flags;
        
        const uint8_t* ptr = data + sizeof(FixedHeader);
        
        // 解析主数据
        uint32_t data_size = header->size;
        if (data_size > 0) {
            if (static_cast<size_t>(ptr - data) + data_size > size) {
                throw std::runtime_error("Data size exceeds buffer");
            }
            
            av_new_packet(&pkt, data_size);
            memcpy(pkt.data, ptr, data_size);
            ptr += data_size;
        }
        
        // 解析 side data
        uint32_t side_data_elems = header->side_data_elems;
        if (side_data_elems > 0) {
            pkt.side_data = static_cast<AVPacketSideData*>(
                av_malloc_array(side_data_elems, sizeof(AVPacketSideData)));
            
            if (!pkt.side_data) {
                throw std::runtime_error("Memory allocation failed");
            }
            
            pkt.side_data_elems = static_cast<int>(side_data_elems);
            
            for (uint32_t i = 0; i < side_data_elems; i++) {
                if (static_cast<size_t>(ptr - data) + sizeof(SideDataHeader) > size) {
                    throw std::runtime_error("Side data header incomplete");
                }
                
                const SideDataHeader* sd_header = 
                    reinterpret_cast<const SideDataHeader*>(ptr);
                ptr += sizeof(SideDataHeader);
                
                pkt.side_data[i].type = static_cast<AVPacketSideDataType>(sd_header->type);
                pkt.side_data[i].size = static_cast<int>(sd_header->size);
                
                if (sd_header->size > 0) {
                    if (static_cast<size_t>(ptr - data) + sd_header->size > size) {
                        throw std::runtime_error("Side data size exceeds buffer");
                    }
                    
                    pkt.side_data[i].data = static_cast<uint8_t*>(av_malloc(sd_header->size));
                    if (!pkt.side_data[i].data) {
                        throw std::runtime_error("Memory allocation failed");
                    }
                    
                    memcpy(pkt.side_data[i].data, ptr, sd_header->size);
                    ptr += sd_header->size;
                }
            }
        }
        
        return pkt;
    }
    
    // 计算序列化后的大小
    static size_t calculateSize(const AVPacket& pkt) {
        size_t size = sizeof(FixedHeader);
        size += pkt.size; // 主数据大小
        
        // side data 大小
        for (int i = 0; i < pkt.side_data_elems; i++) {
            size += sizeof(SideDataHeader);
            size += pkt.side_data[i].size;
        }
        
        return size;
    }

private:
    #pragma pack(push, 1) // 1字节对齐
    
    // 固定头部结构
    struct FixedHeader {
        int64_t pts;
        int64_t dts;
        int64_t duration;
        int64_t pos;
        int32_t stream_index;
        int32_t flags;
        uint32_t size;            // 主数据大小
        uint32_t side_data_elems; // side data 数量
    };
    
    // Side data 头部
    struct SideDataHeader {
        uint32_t type;
        uint32_t size;
    };
    
    #pragma pack(pop) // 恢复默认对齐
};



#endif

